# -*- coding: utf-8 -*-
'''
@author: Grégory Romé
@license: http://www.opensource.org/licenses/mit-license.php The MIT License
@copyright: Copyright (C) 2009-2010 Grégory Romé
'''

import xmlrpclib

class XmlRpcClient():
    '''
    
    '''
    proxy = False

    def __init__(self, url):
        '''
        Constructor
        '''
        self.proxy = xmlrpclib.ServerProxy(url+'/xml_rpc')
        
    def test(self, username, password):
        '''
        Test connection
        '''
        try :
            msg = self.proxy.test.connect(username, password)
        except :
            msg = "Error: try to modify the URI"
            
        return msg
    
    def sendMatchReport(self, username, password, filename):
        '''
        Send match report
        '''
        fin = open(filename, 'rb')
        contents = fin.read()
        fin.close()
        file = xmlrpclib.Binary(contents)

        try:
            msg = self.proxy.calendar.matchreport(username,password,file)
        except:
            msg += "Error: try to modify the URI"
        return msg
    
    def sendReplay(self, username, password, filename):
        '''
        Send replay
        '''
        fin = open(filename, 'rb')
        contents = fin.read()
        fin.close()
        file = xmlrpclib.Binary(contents)

        try:
            msg = self.proxy.calendar.replay(username,password,file)
        except:
            msg += "Error: try to modify the URI"
        return msg
        
